﻿
internal class Constants
{
    internal const string MasterPagePath = "~/Site.Master";
    internal const string PromptsMasterPagePath = "~/Prompt.Master";
}

public string GetDataSetName()
{
	return "ds" + FrameworkObject.DataSetName + "1";
}
public string GetDataAdapterName()
{
	return "da" + FrameworkObject.DataAdapterName + "1";
}

public string GetRootLevelName()
{
	return FrameworkObject.Metadata.RootLevel.Name;
}

public string GetDataTableType()
{
	return FrameworkObject.DataSetName + "." + FrameworkObject.Metadata.RootLevel.Name + "DataTable" ;
}

public string GetDataRowType()
{
	return FrameworkObject.DataSetName + "." + FrameworkObject.Metadata.RootLevel.Name + "Row" ;
}

public string GetLevelName(Level lvl)
{
	return lvl.Name;
}

public string GetTextName(AttributeItem att)
{
	return GetControlWebPrefix( att, FrameworkObject) + att.Name;
}

public string GetDataGridName()
{
	return "grid" + FrameworkObject.ObjectName  ;
}

public string GetDataViewName()
{
	return "dv1";
}

public string GetDataGridProviderName()
{
	return "grid" + FrameworkObject.Metadata.RootLevel.Name ;
}

/// <summary>
/// Return a Web Control Type name for a given  AttributeItem.
/// </summary>
/// <param name="att"> AttributeItem</param>
/// <param name="o"> FrameworkObject</param>
/// <returns> System.Type name</returns>
public static string GetControlWebPrefix( AttributeItem att, FrameworkObject framework)
{
	string prefix = "";
	if (att.IsReadOnly || att.DataType == DeklaritType.Blob )
	{
		prefix = "lbl";
		if (att.DataType==DeklaritType.Blob && att.IsPassword && !att.IsReadOnly ) 
		{
			prefix = "txt";
		}
	}
	else
	{
		if (att.DataType == DeklaritType.Boolean )
			prefix = "chk";
		else
		{
			if (att.IsEnumeration)
			{
				prefix =  "drp";
			}
			else
			{
				if (PromptHelper.IsLowCardinalityPrompt(att, framework) )
				{
					ManyToOne mto = PromptHelper.FindIfIsPrompt(att, framework);
					if (mto!=null)
					{
						prefix = "drp";
					}
				}
				else
				{
					if (att.DataType== DeklaritType.Date || att.DataType== DeklaritType.DateTime )
						prefix = "dtb";
					else
						prefix = "txt";
				}
			}
		}
	}
	return prefix;
}

private static AttributeItem GetComboFromMto(Deklarit.Metadata.Level level, string MtoName)
{
	foreach (AttributeItem att in level.AttributeItems)
	{
		if ( att.IsLowCardinalityForeignKey && !att.IsReadOnly)
		{
			ManyToOne mto = att.GetManyToOne();
			if (mto.Name==MtoName)
			{
				return att;
			}
		}
	}
	return null;
	
}

public string GetTextClientIdName(AttributeItem att)
{
	return FrameworkObject.ObjectName + "Component1_"+ GetControlWebPrefix( att, FrameworkObject) + att.Name;
}

private static int MaxComboLength = 30;
	
public int GetComboTextLength(AttributeItem att, ManyToOne mto)
{
    if (att.DataProviderForValueList != null && att.DataProviderForValueList.DescriptionItem !=null )
    {
        return Math.Min( att.DataProviderForValueList.DescriptionItem.Length , MaxComboLength);
    }	
    else if (mto!= null && att.IsLowCardinalityForeignKeyWithDataProvider && mto.RelatedLookupInfo.DataProviderObject.DescriptionItem!=null ) 
	{
		return Math.Min( mto.RelatedLookupInfo.DataProviderObject.DescriptionItem.Length , MaxComboLength);
	}				
	else if (mto!= null && mto.BusinessComponentObject.DescriptionItem!=null)
	{
		return Math.Min( mto.BusinessComponentObject.DescriptionItem.Length , MaxComboLength);
	}
	else  
	{
		return Math.Min( att.Length, MaxComboLength);
	}

}

private static string ConcatAttributeItems(IEnumerable atts, string separator, bool includeType, bool nullable)
{
	string result ="";
	foreach (AttributeItem att in atts)
	{
        if(result.Length > 0 && separator != null)
        {
             result += separator;  
        }
        if(includeType)
        {
             result += GetCSType(att, nullable) + " ";  
        }
		result += att.Name;
	}
	return result; 
}

private static bool IsNullableType(AttributeItem att)
{
    return att.DataType == DeklaritType.Blob
        || att.DataType == DeklaritType.Text
        || att.DataType == DeklaritType.VarChar
        || att.DataType == DeklaritType.String;
}

private static string GetCSType(AttributeItem att, bool nullable)
{
    return GetCSType(att, nullable, true);
}

private static string GetCSType(AttributeItem att, bool nullable, bool considerEnumeration)
{
    if (att.DataType== DeklaritType.Blob) 
	{
		return "Byte[] Array";
	}
	else if(att.DataType== DeklaritType.Boolean)	
	{
		return "bool" + (nullable ? "?" : "");
	}
	else if(att.DataType== DeklaritType.DateTime || att.DataType== DeklaritType.Date)	
	{
		return "DateTime" + (nullable ? "?" : "");
    }
	else if(att.IsEnumeration && considerEnumeration)	
	{
		return att.EnumerationType + (nullable ? "?" : "");
	}
	else if(att.DataType== DeklaritType.Double)	
	{
	    return "double" + (nullable ? "?" : "");
	}
	else if(att.DataType== DeklaritType.Decimal )	
	{
	    return "decimal" + (nullable ? "?" : "");
	}
	else if(att.DataType== DeklaritType.Int )	
	{
		return "int" + (nullable ? "?" : "");
	}
	else if(att.DataType== DeklaritType.Short)	
	{
		return "Int16" + (nullable ? "?" : "");
	}
	else if(att.DataType== DeklaritType.Long )	
	{
		return "Int64" + (nullable ? "?" : "");
	}
	else if(att.DataType== DeklaritType.Guid )	
	{
		return "Guid" + (nullable ? "?" : "");
	}
	else if(att.DataType== DeklaritType.Text || att.DataType== DeklaritType.VarChar || att.DataType== DeklaritType.String)	
	{
		return "string";
	}
    else
        throw new Exception("DVelop: Unknown data type: " +att.DataType);
}

private static string GetEmptyValueComparison(AttributeItem att, bool nullableValue)
{
    return string.Format("!({0})", GetNotEmptyValueComparison(att, nullableValue));
}

private static string GetNotEmptyValueComparison(AttributeItem att, bool nullableValue)
{
    if(att.DataType== DeklaritType.Text || att.DataType== DeklaritType.VarChar || att.DataType== DeklaritType.String)	
	{
		return string.Format("!string.IsNullOrEmpty({0})", att.Name);
	}
	else if(att.DataType== DeklaritType.Boolean)	
	{
        if(nullableValue)
            return att.Name + ".HasValue";
        else
		    return "false";
	}
	else if(att.DataType== DeklaritType.DateTime || att.DataType== DeklaritType.Date)	
	{
        if(nullableValue)
            return string.Format("{0}.HasValue && {0}.Value != DateTime.MinValue", att.Name);
        else
		    return att.Name + " != DateTime.MinValue";
    }
	else if(att.IsEnumeration )	
	{
        if(nullableValue)
            return att.Name + ".HasValue";
        else
		    return "false";
	}
	else if(att.DataType== DeklaritType.Double
            || att.DataType== DeklaritType.Decimal
            || att.DataType== DeklaritType.Int
            || att.DataType== DeklaritType.Short
            || att.DataType== DeklaritType.Long)	
	{
        if(nullableValue)
            return string.Format("{0}.HasValue && {0}.Value != 0", att.Name);
        else
		    return att.Name + " != 0";
	}
	else if(att.DataType== DeklaritType.Guid )	
	{
        return att.Name + " != null";
	}
    else
        throw new Exception("DVelop: Unknown data type: " + att.DataType);
    
}

private static string GetActionKeysList(ArrayList keys)
{
    string actionKeyList = "";
	foreach(AttributeItem keyAtt in keys)
	{
		if(actionKeyList.Length > 0)
		{
			actionKeyList += ", ";
		}
		actionKeyList += string.Format("{0} = row.{0}", keyAtt.Name);
	}
    return actionKeyList;
}

private static string GetLevelDataRowType(Deklarit.Metadata.FrameworkObject FrameworkObject, Level lvl)
{
    return FrameworkObject.DataSetFullName + "." + lvl.Name + "Row";
}

private static string GetDeklaritResource(string key)
{
    return "<%= Deklarit.Resources.Resources." + key + " %>";
}

private static string GetImagesPathRoot()
{
    return "<%= (string)Session[\"ThemePath\"] %>";
}

private static string GetPromptUrlParameterNames(ManyToOne mto, AttributeItem subAttDescriptionValue)
{
    string str = "";
    int num = 0;
    foreach (AttributeItem item in mto.ForeignKeys)
    {
        num++;
        if (num == mto.ForeignKeys.Count)
        {
            str = str + "'" + item.Name + "'";
        }
        else
        {
            str = str + "'" + item.Name + "' , ";
        }
    }
    if (subAttDescriptionValue != null)
    {
        foreach (AttributeItem item2 in mto.ForeignKeys)
        {
            str = str + " , 'txtPrompt" + item2.Name + "'";
        }
    }
    return str;
}

private static string EmptyExpresionString(DeklaritType dataType)
{
	switch (dataType) 
	{
		case DeklaritType.Short:
		case DeklaritType.Double:
		case DeklaritType.Int:
		case DeklaritType.Decimal:
		case DeklaritType.Long:
			return "0";
		case DeklaritType.DateTime:
		case DeklaritType.Date:
			return System.DateTime.MinValue.ToShortDateString();
		case DeklaritType.Guid :
			return System.Guid.Empty.ToString();
		case DeklaritType.Boolean :
			return "false";
		default:
			return "";
	}					
}

private static string EmptyExpresion(DeklaritType dataType)
{
	switch (dataType) 
	{
		case DeklaritType.Short:
		case DeklaritType.Double:
		case DeklaritType.Int:
		case DeklaritType.Decimal:
		case DeklaritType.Long:
			return "0";
		case DeklaritType.DateTime:
		case DeklaritType.Date:
			return "System.DateTime.MinValue";
		case DeklaritType.Guid :
			return "System.Guid.Empty";
		case DeklaritType.Boolean :
			return "false";
		default:
			return "\"\"";
	}					
}

private static string GetExpresionDelimiter(AttributeItem att)
{
	string delimiter = "";
	if (att.ClrType.Equals(typeof(string)) || att.ClrType.Equals(typeof(System.Guid))) 
	{	
		delimiter = "'";
	}  
	if (att.ClrType.Equals(typeof(DateTime))) 
	{	
		delimiter = "#";
	} 
	return delimiter; 
}

private static string GetReadOnlyExpression(AttributeItem att)
{
    if(att.AutoNumber || att.AutoGenerated)
    {
        return "true";
    }
    else
    {
        return GetReadOnlyExpression(att.IsKey) + " || ViewData[\"ReadOnly_" + att.Name + "\"] != null";
    }
}

private static string GetReadOnlyLevelExpression(Level lvl, AttributeItem att)
{
    if(att.AutoNumber || att.AutoGenerated)
    {
        return "true";
    }
    else
    {
        string readOnlyExpression;
        if(att.IsKey)
        {
	        readOnlyExpression = "row != null && row.RowState != DataRowState.Added";
        }
        else
        {
	        readOnlyExpression = "false";
        }
        return readOnlyExpression;
    }
}

private static string GetReadOnlyExpression(bool attIsKey)
{
    string readOnlyExpression = "((EnumMode)ViewData[\"Mode\"]) != EnumMode.Insert";
    if(!attIsKey)
    {
	    readOnlyExpression += " && ((EnumMode)ViewData[\"Mode\"]) != EnumMode.Update";
    }
    return readOnlyExpression;
}

private static ArrayList GetIncludeOrInferredManyToOneFromKeys(ManyToOne relPrompt, Level lvl)
{
    ArrayList list = new ArrayList();
    foreach (ManyToOne one in lvl.ManyToOneCollection)
    {
        if (one.Name != relPrompt.Name)
        {
            bool flag = false;
            if (IncludeInKeysOrItemsStrict(relPrompt.ForeignKeys, one.ForeignKeys, one.ForeignTableItems))
            {
                flag = true;
                list.Add(one);
            }
            if (((relPrompt.ForeignTablePrimaryKey.Count > 0) && !flag) && IncludeInKeysOrItemsStrict(relPrompt.ForeignTablePrimaryKey, one.ForeignKeys, one.ForeignTableItems))
            {
                list.Add(one);
            }
        }
    }
    return list;
}

private static bool IncludeInKeysOrItemsStrict(ArrayList AttributeKeys, ArrayList ForeignKeys, ArrayList ForeignTableItems)
{
    if (AttributeKeys.Count >= ForeignKeys.Count)
    {
        return IncludeInKeys(AttributeKeys, ForeignTableItems);
    }
    if (!IncludeInKeys(AttributeKeys, ForeignKeys))
    {
        return IncludeInKeys(AttributeKeys, ForeignTableItems);
    }
    return true;
}

private static bool IncludeInKeys(ArrayList AttributeKeys, ArrayList ForeignKeys)
{
    foreach (AttributeItem item in AttributeKeys)
    {
        bool flag = false;
        foreach (AttributeItem item2 in ForeignKeys)
        {
            if (item.Name == item2.Name)
            {
                flag = true;
                break;
            }
        }
        if (!flag)
        {
            return false;
        }
    }
    return true;
}

private static AttributeItem FindAttributeOrSuperType(IEnumerable atts, string attToSearch)
{
	foreach(AttributeItem att in atts)
	{
		if(att.Name.ToLower() == attToSearch.ToLower() || (att.SuperType != null && att.SuperType.ToLower() == attToSearch.ToLower()))
		{
			return att;
		}
	}
    return null;
}

private static bool IsMultiline(AttributeItem att)
{
    return att.DataType == DeklaritType.Text;
}

private static string ConvertFromStringToType(string type, string expressionToCast)
{
	switch (type) 
	{
		case "System.Int16":
		case "System.Int32":
		case "System.Int64":
			return string.Format(type + ".Parse({0})", expressionToCast);
		case "string":
			return expressionToCast;
		default:
			return string.Format("((" + type + ")){0}", expressionToCast);
    }
}

private static bool IsBlobImage(AttributeItem att)
{
    return att.IsBlobImage || att.MimeType .ToLower() == "text/jpg" || att.MimeType .ToLower() == "text/png" || att.MimeType .ToLower() == "text/bmp" || att.MimeType .ToLower() == "text/gif";
}


 


 












